# 切换到 HTTP 模式脚本
# RefundAuditAndroid HTTP/HTTPS 切换工具
# 作者: AI Assistant
# 版本: 1.0

param(
    [switch]$Force = $false
)

# 颜色输出函数
function Write-Success { param([string]$Message) Write-Host $Message -ForegroundColor Green }
function Write-Warning-Custom { param([string]$Message) Write-Host $Message -ForegroundColor Yellow }
function Write-Error-Custom { param([string]$Message) Write-Host $Message -ForegroundColor Red }
function Write-Info { param([string]$Message) Write-Host $Message -ForegroundColor Cyan }

try {
    Write-Info "======================================"
    Write-Info "🔄 切换到 HTTP 模式"
    Write-Info "======================================"
    Write-Host ""
    
    # 检查当前目录
    if (!(Test-Path "vite.config.js") -or !(Test-Path "package.json")) {
        throw "请在 RefundAuditAndroid 项目根目录下运行此脚本"
    }
    
    # 创建备份
    if (!$Force) {
        Write-Info "📋 创建配置文件备份..."
        $timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
        $backupDir = "backup_$timestamp"
        New-Item -ItemType Directory -Path $backupDir -Force | Out-Null
        
        Copy-Item "vite.config.js" "$backupDir/vite.config.js.backup"
        Copy-Item "src/utils/api.js" "$backupDir/api.js.backup"
        Copy-Item "capacitor.config.json" "$backupDir/capacitor.config.json.backup"
        Copy-Item "nginx-refundtask.conf" "$backupDir/nginx-refundtask.conf.backup"
        
        Write-Success "✅ 备份已创建: $backupDir"
    }
    
    Write-Info "🔧 修改配置文件..."
    
    # 1. 修改 vite.config.js
    Write-Info "   修改 vite.config.js"
    $viteConfig = Get-Content "vite.config.js" -Raw
    
    # 注释掉 HTTPS 配置
    $viteConfig = $viteConfig -replace '(\s+)https:\s*\{[^}]+\}', '$1// https: {$1//   key: "./ssl/lzetyy.com.key",$1//   cert: "./ssl/lzetyy.com.pem"$1// }'
    
    # 修改 target 为 HTTP
    $viteConfig = $viteConfig -replace "target: 'https://www\.lzetyy\.com:6731/refundtaskapi'", "target: 'http://www.lzetyy.com:6731/refundtaskapi'"
    
    # 修改 secure 为 false
    $viteConfig = $viteConfig -replace "secure: true", "secure: false"
    
    $viteConfig | Set-Content "vite.config.js" -Encoding UTF8
    
    # 2. 修改 src/utils/api.js
    Write-Info "   修改 src/utils/api.js"
    $apiConfig = Get-Content "src/utils/api.js" -Raw
    $apiConfig = $apiConfig -replace "baseURL: 'https://www\.lzetyy\.com:6731/refundtaskapi/'", "baseURL: 'http://www.lzetyy.com:6731/refundtaskapi/'"
    $apiConfig | Set-Content "src/utils/api.js" -Encoding UTF8
    
    # 3. 修改 capacitor.config.json
    Write-Info "   修改 capacitor.config.json"
    $capacitorConfig = Get-Content "capacitor.config.json" -Raw
    $capacitorConfig = $capacitorConfig -replace '"androidScheme": "https"', '"androidScheme": "http"'
    $capacitorConfig | Set-Content "capacitor.config.json" -Encoding UTF8
    
    # 4. 修改 nginx-refundtask.conf
    Write-Info "   修改 nginx-refundtask.conf"
    $nginxConfig = Get-Content "nginx-refundtask.conf" -Raw
    
    # 注释掉 HTTPS 端口
    $nginxConfig = $nginxConfig -replace "listen 443 ssl http2;", "# listen 443 ssl http2;  # HTTPS模式时取消注释"
    
    # 修改 proxy_pass 为 HTTP
    $nginxConfig = $nginxConfig -replace "proxy_pass https://www\.lzetyy\.com:6731/refundtaskapi/api/;", "proxy_pass http://www.lzetyy.com:6731/refundtaskapi/api/;"
    
    $nginxConfig | Set-Content "nginx-refundtask.conf" -Encoding UTF8
    
    Write-Success "✅ 所有配置文件已修改完成"
    
    Write-Info ""
    Write-Info "📋 HTTP 模式配置完成！"
    Write-Info "======================================"
    Write-Success "🌐 开发服务器地址: http://localhost:8090/refundtask/"
    Write-Success "🌐 生产环境地址: http://www.lzetyy.com/refundtask/"
    Write-Info "======================================"
    Write-Host ""
    
    Write-Info "📝 后续步骤:"
    Write-Host "   1. 运行开发服务器: npm run dev" -ForegroundColor White
    Write-Host "   2. 构建生产版本: npm run build" -ForegroundColor White
    Write-Host "   3. 重启 Nginx: nginx -s reload" -ForegroundColor White
    Write-Host "   4. 切换回 HTTPS: .\switch-to-https.ps1" -ForegroundColor White
    Write-Host ""
    
    Write-Warning-Custom "⚠️  注意: HTTP 模式仅用于开发测试，生产环境请使用 HTTPS 模式"
    
} catch {
    Write-Error-Custom "❌ 错误: $($_.Exception.Message)"
    exit 1
}

Write-Success "🎉 HTTP 模式切换完成！"